package com.wontube.smr.dao.mapper;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.session.RowBounds;

import com.wontube.core.dao.mybatis.QueryExample;
import com.wontube.smr.client.model.po.Movie;
import com.wontube.smr.dao.providers.MovieSqlProvider;


public interface MovieMapper {

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table movie
	 * @mbggenerated
	 */
	int countByExample(QueryExample example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table movie
	 * @mbggenerated
	 */
	int deleteByExample(QueryExample example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table movie
	 * @mbggenerated
	 */
	int deleteByPrimaryKey(String id);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table movie
	 * @mbggenerated
	 */
	int insert(Movie record);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table movie
	 * @mbggenerated
	 */
	int insertSelective(Movie record);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table movie
	 * @mbggenerated
	 */
	List<Movie> selectByExample(QueryExample example, RowBounds rowBounds);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table movie
	 * @mbggenerated
	 */
	List<Movie> selectByExample(QueryExample example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table movie
	 * @mbggenerated
	 */
	Movie selectByPrimaryKey(String id);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table movie
	 * @mbggenerated
	 */
	int updateByExampleSelective(@Param("record") Movie record,
			@Param("example") QueryExample example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table movie
	 * @mbggenerated
	 */
	int updateByExample(@Param("record") Movie record,
			@Param("example") QueryExample example);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table movie
	 * @mbggenerated
	 */
	int updateByPrimaryKeySelective(Movie record);

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table movie
	 * @mbggenerated
	 */
	int updateByPrimaryKey(Movie record);

	// user defined
	@SelectProvider(type = MovieSqlProvider.class, method = "selectWithRecent")
	@ResultMap("BaseResultMap")
	List<Movie> selectWithRecent(QueryExample example, RowBounds rowBounds);

	@SelectProvider(type = MovieSqlProvider.class, method = "selectWithToday")
	@ResultMap("BaseResultMap")
	List<Movie> selectWithToday(QueryExample example, RowBounds rowBounds);

	@SelectProvider(type = MovieSqlProvider.class, method = "selectWithWeek")
	@ResultMap("BaseResultMap")
	List<Movie> selectWithWeek(QueryExample example, RowBounds rowBounds);

	@SelectProvider(type = MovieSqlProvider.class, method = "countWithRecent")
	int countWithRecent(QueryExample example);

	@SelectProvider(type = MovieSqlProvider.class, method = "selectWithRecentByGenre")
	@ResultMap("BaseResultMap")
	List<Movie> selectWithRecentByGenre(@Param("genreName") String genreName, @Param("example") QueryExample example,
			RowBounds rowBounds);

	@SelectProvider(type = MovieSqlProvider.class, method = "selectWithTodayByGenre")
	@ResultMap("BaseResultMap")
	List<Movie> selectWithTodayByGenre(@Param("genreName") String genreName, @Param("example") QueryExample example,
			RowBounds rowBounds);

	@SelectProvider(type = MovieSqlProvider.class, method = "selectWithWeekByGenre")
	@ResultMap("BaseResultMap")
	List<Movie> selectWithWeekByGenre(@Param("genreName") String genreName, @Param("example") QueryExample example,
			RowBounds rowBounds);

	@SelectProvider(type = MovieSqlProvider.class, method = "countWithRecentByGenre")
	int countWithRecentByGenre(@Param("genreName") String genreName, @Param("example") QueryExample example);

	@SelectProvider(type = MovieSqlProvider.class, method = "countWithDayByGenre")
	int countWithDayByGenre(@Param("genreName") String genreName, @Param("example") QueryExample example);

	@SelectProvider(type = MovieSqlProvider.class, method = "countWithWeekByGenre")
	int countWithWeekByGenre(@Param("genreName") String genreName, @Param("example") QueryExample example);

	@SelectProvider(type = MovieSqlProvider.class, method = "countWithToday")
	int countWithToday(QueryExample example);

	@SelectProvider(type = MovieSqlProvider.class, method = "countWithWeek")
	int countWithWeek(QueryExample example);

	@SelectProvider(type = MovieSqlProvider.class, method = "selectWithByGenre")
	@ResultMap("BaseResultMap")
	List<Movie> selectWithByGenre(@Param("genreName") String genreName, @Param("example") QueryExample example,
			RowBounds rowBounds);

	@SelectProvider(type = MovieSqlProvider.class, method = "countWithByGenre")
	int countWithByGenre(@Param("genreName") String genreName, @Param("example") QueryExample example);

	@Update({ "update movie", "set sort_index = sort_index + #{added,jdbcType=INTEGER}",
			"where version < #{maxOldVersion,jdbcType=TIMESTAMP}", "and website = #{website,jdbcType=VARCHAR}",
			"and movie_type = #{movieType,jdbcType=VARCHAR}", "and sort_index < #{maxOldIndex,jdbcType=INTEGER}" })
	int updateOldSortIndex(String website, String movieType, Date maxOldVersion, int added, int maxOldIndex);
	
	@Update({ "update movie set genre=#{genre,jdbcType=VARCHAR} where id=#{movieId,jdbcType=VARCHAR}"})
	int updateGenre(@Param("movieId") String movieId, @Param("genre")String genre);	
	
	@Update({ "update movie set sort_index=#{sortIndex, jdbcType=INTEGER}, version=#{version,jdbcType=TIMESTAMP} where id=#{movieId,jdbcType=VARCHAR}"})
	int updateSortIndex(@Param("movieId") String movieId, @Param("sortIndex") Integer sortIndex, @Param("version") Date version);
}